home *** CD-ROM | disk | FTP | other *** search
/ Tech Arsenal 1 / Tech Arsenal (Arsenal Computer).ISO / tek-01 / lcppb.zip / LCPP04.ZIP / STACK.CPP < prev    next >
C/C++ Source or Header  |  1991-07-03  |  3KB  |  135 lines

  1. // stack.cpp -- Implement a linked list stack
  2.  
  3. //#include <stream.hpp>
  4. #include <iostream.h>
  5. #include <stdlib.h>
  6. //#include <disp.h>
  7. #include "disp.h"
  8. #include <ctype.h>
  9.  
  10. #define FALSE 0
  11. #define TRUE 1
  12.  
  13. struct item {
  14.   int data;            // Could be any data
  15.   item *next;          // Points to next item in list
  16. };
  17. typedef item *itemptr;  // Create itemptr data type
  18.  
  19. // Function prototypes
  20. void push(itemptr newitem, itemptr &list);
  21. void pop(itemptr &newitem, itemptr &list);
  22. void showlist(itemptr p);
  23. void display(void);
  24. void additem(void);
  25. void delitem(void);
  26. void clearscreen(void);
  27.  
  28. itemptr avail;          // The "avail" stack
  29. itemptr itemlist;       // The "item" stack
  30.  
  31. main()
  32. {
  33.   int done = FALSE;    // When TRUE, program ends
  34.   int c;               // User command character
  35.  
  36.   disp_open();
  37.   while (!done) {
  38.     display();
  39.     disp_move(24, 0);
  40.     disp_printf("A-dd, D-elete, Q-uit");
  41.     c = getch();
  42.     switch (toupper(c)) {
  43.       case 'A': additem(); break;
  44.       case 'D': delitem(); break;
  45.       case 'Q': done = TRUE;
  46.     }
  47.   }
  48. }
  49.  
  50. // Push newitem onto list. Modifies list pointer.
  51. void push(itemptr newitem, itemptr &list)
  52. {
  53.   newitem->next = list;      // Item's next now points to list
  54.   list = newitem;            // List now points to new item
  55. }
  56.  
  57. // Pop newitem from list, or create a new item if list is empty
  58. void pop(itemptr &newitem, itemptr &list)
  59. {
  60.   if (list == NULL) {        // If list is empty...
  61.     newitem = new item;     //  Create new item
  62.     newitem->data = -1;     //  Initialize data to -1
  63.   } else {                   // Else...
  64.     newitem = list;         //  Pass back item at top of list
  65.     list = list->next;      //  Adjust list to next item
  66.   }
  67. }
  68.  
  69. // Display contents of list addressed by p
  70. void showlist(itemptr p)
  71. {
  72.   while (p != NULL) {
  73.  
  74. //    disp_printf("\n%d", p->data);
  75.     disp_move(wherey(), 0);
  76.     disp_printf("%d", p->data);
  77.  
  78.     disp_eeol();
  79.     p = p->next;      // Address next item in the list
  80.   }
  81. }
  82.  
  83. // Display the avail and item stacks
  84. void display(void)
  85. {
  86.   clearscreen();
  87.  
  88. //  disp_printf("\n\nAvail list:");
  89.   disp_move(wherey() + 1, 0);
  90.   disp_printf("Avail list:");
  91.   
  92.   showlist(avail);
  93.  
  94. //  disp_printf("\n\nItem list:");
  95.   disp_move(wherey() + 1, 0);
  96.   disp_printf("Item list:");
  97.  
  98.   showlist(itemlist);
  99. }
  100.  
  101. // Add item with random data to item stack
  102. void additem(void)
  103. {
  104.   itemptr newitem;
  105.  
  106.   pop(newitem, avail);       // Get or create new item
  107.   if (newitem->data < 0)     // If this is a new item
  108.     newitem->data = rand(); // Assign random data to it
  109.   push(newitem, itemlist);   // Put new item onto item stack
  110. }
  111.  
  112. // Delete item from item stack. Save same item on avail stack
  113. void delitem(void)
  114. {
  115.   itemptr newitem;
  116.  
  117.   pop(newitem, itemlist);    // Get new item from item stack
  118.   push(newitem, avail);      // Save item on avail stack
  119. }
  120.  
  121. // Clear the display
  122. void clearscreen(void)
  123. {
  124.   disp_move(0, 0);
  125.   disp_eeop();
  126. }
  127.  
  128.  
  129. // Copyright (c) 1990 by Tom Swan. All rights reserved
  130. // Revision 1.00    Date: 08/29/1990   Time: 08:59 pm
  131.  
  132. // Revision 1.01    Date: 07/03/1991   Time: 04:00 pm
  133. // Converted for Borland C++ 2.0
  134.  
  135.